package com.nine.pesys.model.action;

import com.nine.pesys.base.BaseAction;
import com.nine.pesys.model.entity.*;
import com.nine.pesys.util.annotation.Privilege;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.convention.annotation.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;


/**
 * Created by L先森 on 2014/12/27.
 */
@Controller
@Scope("prototype")
@Namespace("/equipmentImformation")
@ParentPackage("my_struts")
@Results({
        @Result(name = "borrowEquipment",location = "/")
})
@Privilege
public class EquipmentImformationAction extends BaseAction<EquipmentImformation> {

    private String equipmentName;
    private Long equipmentNum;
    private Long gameId;
    private Long eqequipmentDetailId;

    /**
     * 器材借用
     * 需要的参数：用户Id 器材名字  器材数量
     * 需要操作的参数:器材总数减n
     *                          器材细节里面要选出相应的数量设为isBorrow
     * @return
     * @throws Exception
     */
    @Action("/borrowEquipment")
    public String borrowEquipment() throws Exception{
//        先处理主要的Info
//     器材总数减n
        Equipment equipment = new Equipment();
        equipment.setName(equipmentName);
        equipment = this.equipmentService.getByCondition(equipment).get(0);
//        选出对应数量的器材
        equipmentDetails = this.equipmentDetailService.getTheNum(equipment,equipmentNum);
//        找出对应的用户
        User user = (User) session.get("user");
        if (equipmentDetails!=null&&equipmentDetails.size()==equipmentNum){
            for (EquipmentDetail equipmentDetail : equipmentDetails){
                EquipmentImformation equipmentImformation = new EquipmentImformation();
                equipmentImformation.setUser(user);
                equipmentImformation.setEquipmentDetail(equipmentDetail);
                //            借完之后改变相应的状态
                equipmentDetail.setIsBorrow(true);
                this.equipmentDetailService.save(equipmentDetail);
                this.equipmentImformationService.save(equipmentImformation);
            }
            equipment.setLeftNum(equipment.getLeftNum()-equipmentNum);
            this.equipmentService.update(equipment);
            return "borrowEquipment";
        }
        ActionContext.getContext().put("message","所借器材超过剩余数量");
        return "error";
    }

    /**
     * 归还的时候多了两个数据cost 和 isOvaerDate 所以还是会保存一下
     * 一个一个的还
     * 器材归还
     * @return
     * @throws Exception
     */
    @Action("/returnRequipment")
    private String returnRequipment() throws Exception{
//        先保存一下   改变状态移除(equipmentImformation的状态)
        model.setIsReturn(true);
        this.equipmentImformationService.update(model);
//      找出equipmentDetail
        EquipmentDetail equipmentDetail = this.equipmentDetailService.getById(eqequipmentDetailId);
//        改变状态移除(equipmentDetail的状态)
        equipmentDetail.setIsBorrow(false);
        this.equipmentDetailService.update(equipmentDetail);
//        改变Equipment的状态
        //        找出Equipment
        Equipment equipment = new Equipment();
        equipment.setName(equipmentName);
        equipment = this.equipmentService.getByCondition(equipment).get(0);
        equipment.setLeftNum(equipment.getLeftNum()+1);
        this.equipmentService.update(equipment);
        return "returnRequipment";
    }

    public Long getEqequipmentDetailId() {
        return eqequipmentDetailId;
    }

    public void setEqequipmentDetailId(Long eqequipmentDetailId) {
        this.eqequipmentDetailId = eqequipmentDetailId;
    }

    public Long getGameId() {
        return gameId;
    }

    public void setGameId(Long gameId) {
        this.gameId = gameId;
    }

    public String getEquipmentName() {
        return equipmentName;
    }

    public void setEquipmentName(String equipmentName) {
        this.equipmentName = equipmentName;
    }

    public Long getEquipmentNum() {
        return equipmentNum;
    }

    public void setEquipmentNum(Long equipmentNum) {
        this.equipmentNum = equipmentNum;
    }
}
